<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
    <!-- 面包屑end -->
      </div>
      <!-- 数据筛选表单 -->
    <el-form v-loading="loading" ref="form" :model="form" label-width="40px" size="mini">
      <el-form-item label="状态">
        <el-radio-group v-model="status">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="0">草稿</el-radio>
          <el-radio :label="1">待审核</el-radio>
          <el-radio :label="2">审核通过</el-radio>
          <el-radio :label="3">审核失败</el-radio>
          <el-radio :label="4">已删除</el-radio>
        </el-radio-group>
      </el-form-item>
       <el-form-item label="频道">
        <el-select v-model="channelId" placeholder="请选择频道">
          <el-option label="全部频道" :value="null"></el-option>
          <el-option :label="channel.name" :value="channel.id" v-for="(channel, index) in channels" :key="index"></el-option>
          <!-- <el-option label="区域二" value="beijing"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="rangeDate"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['12:00:00']"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd">
    </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :disabled="loading" @click="loadArticles()">筛选</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据筛选表单end -->
    </el-card>
    <!-- 数据表格 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到 {{ totalCount }} 条结果:
      </div>
      <el-table
      :data="articles"
      stripe
      size="mini"
      class="list-table"
      style="width: 100%"
      v-loading="loading">
      <el-table-column
        prop="date"
        label="封面">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.cover.images[0]"
            fit="cover"
            lazy>
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <!-- <img v-if="scope.row.cover.images[0]" :src="scope.row.cover.images[0]" alt="" class="article-cover">
          <img v-else src="./zw.jpg" alt="" class="article-cover"> -->
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题">
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.status === 0" type="warning">草稿</el-tag>
          <el-tag v-if="scope.row.status === 1">待审核</el-tag>
          <el-tag v-if="scope.row.status === 2" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.status === 3" type="danger">审核失败</el-tag>
          <el-tag v-if="scope.row.status === 4" type="info">已删除</el-tag> -->
           <el-tag :type="articleStatus[scope.row.status].type">{{articleStatus[scope.row.status].text}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="pubdate"
        label="发布时间">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
        <el-button
          type="primary" icon="el-icon-edit" circle @click="$router.push('/publish?id=' + scope.row.id)"></el-button>
        <el-button
          type="danger" icon="el-icon-delete" circle @click="onDeleteArticle(scope.row.id)"></el-button>
      </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格end -->
    <!-- 分页 -->
    <el-pagination
    layout="prev, pager, next"
    background
    :total="totalCount"
    :page-size="pageSize"
    @current-change="onCurrentChange"
    :disabled="loading"
    :current-page.sync="page">
    </el-pagination>
    <!-- 分页end -->
    </el-card>
  </div>
</template>

<script>
import { getArticles, getArticleChannels, deleteArticle } from '@/api/article'
// import { getArticleChannels } from '@/api/article'
export default {
  name: 'ArticleIndex',
  components: {},
  props: {},
  data () {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      articles: [],
      articleStatus: [
        { text: '草稿', type: 'info' },
        { text: '待审核', type: '' },
        { text: '审核通过', type: 'success' },
        { text: '审核失败', type: 'warning' },
        { text: '已删除', type: 'danger' }
      ],
      totalCount: 0,
      pageSize: 20,
      status: null,
      channels: [],
      channelId: null,
      rangeDate: [],
      loading: true,
      page: 1
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadArticles()
    this.loadChannels()
  },
  mounted () {},
  methods: {
    onSubmit () {
      console.log('submit!')
    },
    loadArticles (page = 1) {
      this.loading = true
      getArticles({
        page: page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null,
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null
      }).then(res => {
        const { results, total_count: totalCount } = res.data.data
        this.articles = results
        this.totalCount = totalCount
        this.loading = false
        // this.articles = res.data.data.results
      })
    },
    onCurrentChange (page) {
      this.loadArticles(page)
      // console.log(page)
    },
    loadChannels () {
      getArticleChannels().then(res => {
        // console.log(res)
        this.channels = res.data.data.channels
      })
    },
    onDeleteArticle (articleId) {
      this.$confirm('此操作将删除此文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArticle(articleId.toString()).then(res => {
          this.loadArticles(this.page)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .filter-card {
      margin-bottom: 30px;
  }
  .list-table {
    margin-bottom: 20px;
  }
  .article-cover {
    width: 100px;
    background-size: cover;
  }
  .image-slot {
    text-align: center;
    line-height: 100px;
  }
</style>>
